import java.util.Arrays;
import java.util.Objects;

public class HashBuck {
    class Node{
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    public Node[] array=new Node[10];
    public int usdeSize;

    @Override
    public String toString() {
        return "HashBuck{" +
                "array=" + Arrays.toString(array) +
                ", usdeSize=" + usdeSize +
                '}';
    }

    //负载因子，java默认是0.75
    public static final float LOAD_FACTOR = 0.75f;
    //哈希存储
    public void put(int key,int val) {
        //1.通过哈希函数找到存储位置
        int index=key % array.length;

        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                cur.val=val;
                return;
            }
            cur=cur.next;
        }

        //头插该节点
        Node node=new Node(key,val);
        node.next=array[index];
        array[index]=node;
        usdeSize++;

        if(doLoadFactor()>=0.75){
            reSize();
        }

    }
    private void reSize(){
        Node[] newArray=new Node[array.length*2];
        for (int i = 0; i < array.length; i++) {
            Node cur=array[i];
            while(cur!=null){
                int index=cur.key% newArray.length;
                if(newArray[index]==null){
                    newArray[index]=cur;
                }else{
                    cur.next=newArray[index];
                    newArray[index]=cur;

                }
                cur=cur.next;
            }
        }
        array=Arrays.copyOf(newArray,newArray.length);
    }
    private float doLoadFactor(){
        return usdeSize*1.0f/array.length;
    }
    public int get(int key){
        int index=key%array.length;
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                return cur.val;
            }else{
                cur=cur.next;
            }
        }
        return -1;
    }
}
